package main

import (
	"fmt"
	"time"
)

func main() {
	//打印时间戳
	fmt.Println(time.Now().Unix())

	//创建一个定时器
	myTimer := time.NewTimer(time.Second * 1)
	now := <-myTimer.C
	fmt.Println(now.Unix())

	after()

	stop()
}

func after() {
	//time.After,等价于NewTimer.C
	now := <-time.After(time.Second * 1)
	fmt.Println(now.UnixNano())
}

func stop() {
	myTimer := time.NewTimer(time.Second * 5)
	go func() {
		fmt.Println("子go程，定时开始")
		<-myTimer.C
		fmt.Println("子go程，定时完毕")
	}()

	//停止定时器
	myTimer.Stop()
	//重设定时器
	myTimer.Reset(time.Second * 2)

	//循环定时
	myTicker()

	//for {
	//	;
	//}
}

func myTicker() {
	myTicker := time.NewTicker(time.Second)
	quit := make(chan bool)
	go func() {
		i := 0
		for {
			nowTime := <-myTicker.C
			fmt.Println("now time:", nowTime)
			i++
			if i == 8 {
				quit <- true
				break
			}
		}
	}()
	<-quit
}
